package ubo.master1.jeupoker.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Afichage action des utilisateurs, 
 * Le tapi , les mises 
 */

public class TableauDeBord extends JPanel {

    private static final int NO_OF_CARDS = 5;
    /** Fenetre action utilisateur. */
    private final ControlPrincipal controlPanel;
    /** Label pour la mise actuel. */
    private final JLabel betLabel;
    /** Label pour le pot du tapi. */
    private final JLabel potLabel;
    /** Label des carte du tapi. */
    private final JLabel[] cardLabels;
    /** Label d'affichage des action utilisateur. */
    private final JLabel messageLabel;

    /**
     * Constructeur.
     * 
     * @param mainFrame
     *            Fenetre carte et message action utilisateurs.
     */
    public TableauDeBord(ControlPrincipal controlPanel) {
        this.controlPanel = controlPanel;
        this.setOpaque(false);
        setBackground(UIConfiguration.TABLE_COLOR);
        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel label = new JLabel("Bet");
        label.setForeground(Color.GREEN);
        gc.gridx = 1;
        gc.gridy = 0;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.NONE;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 0, 5);
        add(label, gc);

        label = new JLabel("Pot");
        label.setForeground(Color.GREEN);
        gc.gridx = 3;
        gc.gridy = 0;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.NONE;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 0, 5);
        add(label, gc);

        betLabel = new JLabel();
        betLabel.setBorder(UIConfiguration.LABEL_BORDER);
        betLabel.setForeground(Color.GREEN);
        betLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 1;
        gc.gridy = 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 5, 5);
        add(betLabel, gc);

        potLabel = new JLabel();
        potLabel.setBorder(UIConfiguration.LABEL_BORDER);
        potLabel.setForeground(Color.GREEN);
        potLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 3;
        gc.gridy = 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(5, 5, 5, 5);
        add(potLabel, gc);

        // Position des cinq cartes.
        cardLabels = new JLabel[NO_OF_CARDS];
        for (int i = 0; i < 5; i++) {
            cardLabels[i] = new JLabel(AcceeCarts.getIcon("/images/card_back.png"));
            gc.gridx = i;
            gc.gridy = 2;
            gc.gridwidth = 1;
            gc.gridheight = 1;
            gc.weightx = 0.0;
            gc.weighty = 0.0;
            gc.anchor = GridBagConstraints.CENTER;
            gc.fill = GridBagConstraints.NONE;
            gc.insets = new Insets(10, 5, 10, 5);
            add(cardLabels[i], gc);
        }

        // Message label.
        messageLabel = new JLabel();
        messageLabel.setForeground(Color.YELLOW);
        messageLabel.setHorizontalAlignment(JLabel.CENTER);
        gc.gridx = 0;
        gc.gridy = 3;
        gc.gridwidth = 5;
        gc.gridheight = 1;
        gc.weightx = 1.0;
        gc.weighty = 0.0;
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.insets = new Insets(10, 0, 5, 0);
        add(messageLabel, gc);

        // Control panel.
        gc.gridx = 0;
        gc.gridy = 4;
        gc.gridwidth = 5;
        gc.gridheight = 1;
        gc.weightx = 1.0;
        gc.weighty = 1.0;
        gc.insets = new Insets(5, 0, 5, 0);
        gc.anchor = GridBagConstraints.CENTER;
        gc.fill = GridBagConstraints.BOTH;
        add(controlPanel, gc);
        // Initialisation des carte lors du premier affichage 
        update(null, 0, 0);
    }    
    
    /**
     * Mise a jour de la main .
     * 
     * @param bet
     *            La mise.
     * @param pot
     *            Le pot.
     */
    public void update(List<LaCart> cards, int bet, int pot) {
        if (bet == 0) {
            betLabel.setText("10");
        } else {
            betLabel.setText("$ " + bet);
        }
        if (pot == 0) {
            potLabel.setText(" 100");
        } else {
            potLabel.setText("$ " + pot);
        }
        int noOfCards = (cards == null) ? 0 : cards.size();
        for (int i = 0; i < NO_OF_CARDS; i++) {
            if (i < noOfCards) {
                cardLabels[i].setIcon(AcceeCarts.getCardImage(cards.get(i)));
            } else {
                cardLabels[i].setIcon(AcceeCarts.getIcon("/images/card_back.png"));
            }
        }

    }
    
     /**
     * Afficher un message.
     * 
     * @param message
     *            Le message.
     */
    public void setMessage(String message) {
        if (message.length() == 0) {
            messageLabel.setText(" ");
        } else {
            messageLabel.setText(message);
        }
    }
    
    /**
     * Attendre l'action de l'utilisateur.
     */
    public void waitForUserInput() {
        controlPanel.waitForUserInput();
    }
}
